<?php
namespace App\Controller\Admin;

use App\Query\UserQuery;
use App\Query\ConfigQuery;
use App\Query\UserLogQuery;
use App\Query\UserPayQuery;
use App\Query\UserTransferQuery;
use App\Repository\UserRepository;
use App\Request\UserUpdateRequest;

use App\Job\UserMinRedJob;

class UserController extends Controller
{
    // 列表
    public function index()
    {
        return UserQuery::query(
            UserQuery::model(),
            $this->inputs('username', 'user_id', 'shop_id', 'level_id', 'phone')
        )->paginate(15)->toArray();
    }
    
    // 更新
    public function update($id, UserUpdateRequest $request)
    {
        $model = UserRepository::find($id);

        if($model && $model->update($request->validated()))
        {
            $this->message('update');
        }
        
        $this->error('update');
    }
    
    // 使用余额
    public function moneyMake($id)
    {
        $user = UserRepository::find($id);
    
        if($user && $user->moneyMake($this->input('money'), $this->input('content')))
        {
            // 远程积分
            $user->moneyIntegral(-1 * $this->input('money'));
            $this->message('ok');
        }
        
        $this->error('error');
    }
    
    // 派送优惠
    public function moneyDiscount($id)
    {
        $user = UserRepository::find($id);
    
        if($user && $user->moneyDiscountAdd($this->input('money'), $this->input('content')))
        {
            $this->message('ok');
        }
        
        $this->error('error');
    }
    
    // 满足最小区用户
    public function moneyUserMin()
    {
        return UserQuery::moneyUserMin(ConfigQuery::get()['user_money_user_min'])->get();
    }
    
    // 最小区分红
    public function moneyUserMinRed()
    {
        (new UserMinRedJob)->push($this->input('ids'), $this->input('money'));
        
        $this->message('ok');
    }
    
    // 日志
    public function log()
    {
        return UserLogQuery::query(
            UserLogQuery::model(),
            $this->inputs('user_id', 'type', 'money_type', 'content', 'time')
        )->paginate(15)->toArray();
    }
    
    // 充值
    public function pay()
    {
        return UserPayQuery::query(
            UserPayQuery::model(),
            $this->inputs('user_id', 'shop_id', 'type', 'code', 'time')
        )->paginate(15)->toArray();
    }
    
    // 转帐
    public function transfer()
    {
        return UserTransferQuery::query(
            UserTransferQuery::model(),
            $this->inputs('user_id', 'to_user_id', 'money_type', 'time')
        )->paginate(15)->toArray();
    }
}